package game;



import util.PermutationGenerator;

public class PayOffMatrix {

	

	private double payOffMatrix[][][];

	// action type
	public static final int COOPERATE = 1;

	public static final int DEFEACT = 0;

	// some special games
	public static final int STAG_HUNT = 19; 

	public static final int GAME_OF_CHICKEN = 15; 

	public static final int PRISONERS_DILLEMMA = 13; 
	
	public static final double BValue = Constants.BVALUE; 
	

	public static double P ;
	public static double R ;
	public static double S ;
	public static double T ;
	/**
	 * 
	 */
	public PayOffMatrix() {
		T = Constants.BVALUE;//1.0 + Constants.BVALUE;
		R = 1.0;
		S = 0.0;//1.0 - Constants.BVALUE;
		P= 0.0;

		// total 24 different game, each having 2*2 action combination
		payOffMatrix = new double[2][2][24];


		double[] elements = new double[4];

		elements[0] = P;
		elements[1] = S;
		elements[2] = R;
		elements[3] = T;
		//for (int i = 0; i < 4; i++)
			//elements[i] = i;

		PermutationGenerator x = new PermutationGenerator(elements.length);
		int[] indices;
		int i=0;
		while (x.hasMore()) {
			indices = x.getNext();
		
			int[] copyIndicies = (int[]) indices.clone();
			payOffMatrix[0][0][i] = elements [ indices[3]] ;
			payOffMatrix[0][1][i] = elements [ indices[2] ];
			payOffMatrix[1][0][i] = elements [ indices[1] ];
			payOffMatrix[1][1][i] = elements [ indices[0] ];
			i++;
			
		}


	}


	public static void main(String[] args) {
		PayOffMatrix p = new PayOffMatrix();
		for (int i = 0; i < 24; i++) {
			System.out.println(p.toString(i));
		}
		System.err.println("end");

	}

	public float getFirstScore(int gameID,int action1, int action2) {
		return (float) payOffMatrix[action1][action2][gameID];
	}

	public float getSecondScore(int gameID,int action1, int action2) {

		return (float) payOffMatrix[action2][action1][gameID];
	}

	

	private static PayOffMatrix singleton = new PayOffMatrix();

	public static PayOffMatrix getSingleton() {
		return singleton;
	}

	public String toString(int gameID) {
		StringBuffer stringBuffer = new StringBuffer();
		if(gameID == GAME_OF_CHICKEN)
			stringBuffer.append(" game: GAME OF CHICKEN ");	
		else if(gameID == PRISONERS_DILLEMMA)
			stringBuffer.append(" game: PRISONERS DILLEMMA ");	
		else if(gameID == STAG_HUNT)
			stringBuffer.append(" game: STAG HUNT ");
		stringBuffer.append("\n");
		System.out.println("index :" + gameID + " [C][C]="
				+ payOffMatrix[1][1][gameID] + " [C][D]="
				+ payOffMatrix[1][0][gameID] + " [D][C]="
				+ payOffMatrix[0][1][gameID] + " [D][D]="
				+ payOffMatrix[0][0][gameID]);
		return stringBuffer.toString();

	}

}
